#include <stdio.h>
#include <unistd.h>

#define S_LINK_OLD_NAME "05_slink"
#define S_LINK_NEW_NAME "05_slink_new"

int main(int argc, char const *argv[])
{
    int iRet;
    char strBuffer[200] = {0};

    iRet = readlink( S_LINK_OLD_NAME , strBuffer, sizeof(strBuffer) );
    if ( -1 == iRet )
    {
        perror( "fail readlink" );
    }
    printf( "1st readlink from %s: %s\n", S_LINK_OLD_NAME, strBuffer );


    iRet = rename( S_LINK_OLD_NAME, S_LINK_NEW_NAME );
    if ( -1 == iRet )
    {
        perror( "fail rename" );
    }
    printf( "rename from %s to %s\n", S_LINK_OLD_NAME, S_LINK_NEW_NAME );

    iRet = readlink( S_LINK_NEW_NAME, strBuffer, sizeof(strBuffer) );
    if ( -1 == iRet )
    {
        perror( "fail readlink" );
    }
    printf( "2nd readlink from %s: %s\n", S_LINK_NEW_NAME, strBuffer );

    sleep( 5 );

    iRet = rename( S_LINK_NEW_NAME, S_LINK_OLD_NAME  );
    if ( -1 == iRet )
    {
        perror( "fail rename" );
    }
    printf( "rename from %s to %s\n", S_LINK_NEW_NAME, S_LINK_OLD_NAME );


    return 0;
}

/*
程序功能:
    读取05_slink.c 程序中创建的链接文件 05_slink 所指向的文件名
    更改 05_slink 的文件名为新文件名 05_slink_new
    并打印 新链接文件 05_slink_new 所指向的文件名.
    再将文件名更改回 05_slink.

涉及函数
    readlink
    rename

知识要点
    man 2 rename
    man 2 readlink

*/